import 'package:flutter/material.dart';
import '../screens/home/home_screen.dart';
import '../screens/video/video_screen.dart';
import '../screens/adoption/adoption_screen.dart';
import '../screens/community/community_screen.dart';
import '../screens/profile/profile_screen.dart';
import '../widgets/bottom_nav_bar.dart';

class MainScreen extends StatefulWidget {
  @override
  _MainScreenState createState() => _MainScreenState();
}

class _MainScreenState extends State<MainScreen> {
  int _currentIndex = 0;
  
  final List<Widget> _screens = [
    HomeScreen(),
    VideoScreen(),
    AdoptionScreen(),
    CommunityScreen(),
    ProfileScreen(),
  ];

  final List<String> _titles = [
    '宠物乐园',
    '宠物视频',
    '宠物领养',
    '宠物社区',
    '个人中心',
  ];

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () async {
        // 如果不在首页，先返回首页
        if (_currentIndex != 0) {
          setState(() {
            _currentIndex = 0;
          });
          return false;
        }
        // 如果在首页，则提示是否退出应用
        return await _showExitConfirmDialog() ?? false;
      },
      child: Scaffold(
        body: IndexedStack(
          index: _currentIndex,
          children: _screens,
        ),
        bottomNavigationBar: BottomNavBar(
          currentIndex: _currentIndex,
          onTap: (index) {
            setState(() {
              _currentIndex = index;
            });
          },
        ),
      ),
    );
  }

  Future<bool?> _showExitConfirmDialog() {
    return showDialog<bool>(
      context: context,
      builder: (context) => AlertDialog(
        title: Text('确认退出'),
        content: Text('您确定要退出宠物乐园吗？'),
        actions: [
          TextButton(
            onPressed: () => Navigator.of(context).pop(false),
            child: Text('取消'),
          ),
          TextButton(
            onPressed: () => Navigator.of(context).pop(true),
            child: Text('退出'),
          ),
        ],
      ),
    );
  }
} 